package org.example.ExperimentFifth;

import java.util.HashMap;
import java.util.Map;

public class FindNumber {
    public static int findMost(int[] nums) {
        Map<Integer, Integer> frequencyMap = new HashMap<>();
//最大次数和元素
        int maxTime = 0;
        int Element = 0;

        for (int num : nums) {
            frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
            int frequency = frequencyMap.get(num);
            if (frequency > maxTime) {
                maxTime = frequency;
                Element = num;
            }
        }

        return Element;
    }

    public static void main(String[] args) {
        int[] nums1 = {1, 3, 2, 3, 4, 1, 3};
        System.out.println("输入：nums = [1, 3, 2, 3, 4, 1, 3]");
        System.out.println("输出：" + findMost(nums1));

        int[] nums2 = {4, 4, 1, 1, 2, 2};
        System.out.println("输入：nums = [4, 4, 1, 1, 2, 2]");
        System.out.println("输出：" + findMost(nums2));
    }
}